﻿@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@{
    foreach (var namespaceName in Model.RequiredNamespaces)
    {
@:using @namespaceName;
    }
}

namespace @Model.ControllerNamespace
{
@{
    string routePrefix = "api/" + Model.ControllerRootName;
    var entitySetName = Model.ModelMetadata.EntitySetName;
    var primaryKeyName = Model.ModelMetadata.PrimaryKeys[0].PropertyName;
    var primaryKeyShortTypeName = Model.ModelMetadata.PrimaryKeys[0].ShortTypeName;
    var primaryKeyType = Model.ModelMetadata.PrimaryKeys[0].TypeName;
    var primaryKeyIsAutoGenerated = Model.ModelMetadata.PrimaryKeys[0].IsAutoGenerated;
}
    [Route("api/[controller]")]
    [ApiController]
    public class @Model.ControllerName : ControllerBase
    {
        private readonly @Model.ContextTypeName _context;

        public @(Model.ControllerName)(@Model.ContextTypeName context)
        {
            _context = context;
        }

        // GET: @routePrefix
        [HttpGet]
        public async Task<ActionResult<IEnumerable<@(Model.ModelTypeName)>>> Get@(entitySetName)()
        {
            return await _context.@(entitySetName).ToListAsync();
        }

        // GET: @routePrefix/5
        [HttpGet("{id}")]
        public async Task<ActionResult<@(Model.ModelTypeName)>> Get@(Model.ModelTypeName)(@primaryKeyShortTypeName id)
        {
            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);

            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            return @Model.ModelVariable;
        }

        // PUT: @routePrefix/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> Put@(Model.ModelTypeName)(@primaryKeyShortTypeName id, @Model.ModelTypeName @Model.ModelVariable)
        {
            if (id != @(Model.ModelVariable).@primaryKeyName)
            {
                return BadRequest();
            }

            _context.Entry(@Model.ModelVariable).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!@(Model.ModelTypeName)Exists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: @routePrefix
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<@(Model.ModelTypeName)>> Post@(Model.ModelTypeName)(@Model.ModelTypeName @Model.ModelVariable)
        {
            _context.@(entitySetName).Add(@Model.ModelVariable);
@{
    if (primaryKeyIsAutoGenerated)
    {
            @:await _context.SaveChangesAsync();
    }
    else
    {
            @:try
            @:{
                @:await _context.SaveChangesAsync();
            @:}
            @:catch (DbUpdateException)
            @:{
                @:if (@(Model.ModelTypeName)Exists(@(Model.ModelVariable).@primaryKeyName))
                @:{
                    @:return Conflict();
                @:}
                @:else
                @:{
                    @:throw;
                @:}
            @:}
    }
}

            return CreatedAtAction("Get@(Model.ModelTypeName)", new { id = @(Model.ModelVariable).@primaryKeyName }, @Model.ModelVariable);
        }

        // DELETE: @routePrefix/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete@(Model.ModelTypeName)(@primaryKeyShortTypeName id)
        {
            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);
            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            _context.@(entitySetName).Remove(@Model.ModelVariable);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool @(Model.ModelTypeName)Exists(@primaryKeyShortTypeName id)
        {
            return _context.@(entitySetName).Any(e => e.@primaryKeyName == id);
        }
    }
}
